﻿using System;
using System.Globalization;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Resources;
using Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModelBase;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Views.Catalogs.Acl;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Acl
{
	/// <summary>
	/// 
	/// </summary>
	public partial class AclViewModel
		: ObservableObject
	{

		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private AclView _aclView;

		#endregion

		#region Constructor

		/// <summary>
		/// 
		/// </summary>
		/// <param name="aclView"></param>
		public AclViewModel(AclView aclView)
		{
			if (aclView == null)
				throw new ArgumentNullException("aclView");
			_aclView = aclView;
			WireUpEvents();
			InitModels();
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		private void WireUpEvents()
		{
			PropertyChanged += AclViewModel_PropertyChanged;
			_aclView.Load += LoadView;
			_aclView.closeButton.Click += CloseView;
			_aclView.bindingNavigatorAddNewItem.Click += AddNewAcl;
			_aclView.newButton.Click += AddNewAcl;
			_aclView.saveButton.Click += SaveAcl;
			_aclView.aclBindingNavigatorSaveItem.Click += SaveAcl;
			_aclView.bindingNavigatorDeleteItem.Click += DeleteAcl;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void LoadView(object sender, EventArgs e)
		{
			_aclView.Text = string.Format(
				CultureInfo.InvariantCulture,
				WindowTitles.AclWindowTitle
			);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void CloseView(object sender, EventArgs e)
		{
			_aclView.Close();
		}

		#endregion

		#region Private methods

		/// <summary>
		/// 
		/// </summary>
		private void InitModels()
		{
			LoadAcls();
		}

		/// <summary>
		/// 
		/// </summary>
		private void StartWaiting()
		{
			_aclView.WaitingBar.StartWaiting();
		}

		/// <summary>
		/// 
		/// </summary>
		private void EndWaiting()
		{
			_aclView.WaitingBar.EndWaiting();
		}

		/// <summary>
		/// 
		/// </summary>
		private void FocusDefaultControl()
		{
			_aclView.ActiveControl = _aclView.nombreUltraTextEditor;
		}

		#endregion

	}
}
